package com.example.utils;

import com.example.model.MachineTx;
import com.example.model.Machine_test;
import com.example.temp.GzipCompressJson;
import com.example.temp.Sha256BarcodeValue;
import com.fasterxml.jackson.core.type.TypeReference;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MachineTestToUploadDataUtil {
//    public static void main(String[] args) {
//        String data = "[{\"machinedata\":\"左1\",\"TestPoint\":\"左1\",\"Value\":\"1.073\"},{\"machinedata\":\"左2\",\"TestPoint\":\"左2\",\"Value\":\"1.014\"},{\"machinedata\":\"左3\",\"TestPoint\":\"左3\",\"Value\":\"1.014\"},{\"machinedata\":\"上1\",\"TestPoint\":\"上1\",\"Value\":\"1.042\"},{\"machinedata\":\"上2\",\"TestPoint\":\"上2\",\"Value\":\"1.120\"},{\"machinedata\":\"上3\",\"TestPoint\":\"上3\",\"Value\":\"1.050\"},{\"machinedata\":\"右1\",\"TestPoint\":\"右1\",\"Value\":\"1.021\"},{\"machinedata\":\"右2\",\"TestPoint\":\"右2\",\"Value\":\"1.024\"},{\"machinedata\":\"右3\",\"TestPoint\":\"右3\",\"Value\":\"1.080\"},{\"machinedata\":\"2.79\",\"TestPoint\":\"2.79\",\"Value\":\"2.857\"},{\"machinedata\":\"厚度\",\"TestPoint\":\"厚度\",\"Value\":\"0.605\"},{\"machinedata\":\"1.8\",\"TestPoint\":\"1.8\",\"Value\":\"1.810\"},{\"machinedata\":\"上\",\"TestPoint\":\"上\",\"Value\":\"0.323\"},{\"machinedata\":\"右\",\"TestPoint\":\"右\",\"Value\":\"0.319\"},{\"machinedata\":\"下\",\"TestPoint\":\"下\",\"Value\":\"0.309\"},{\"machinedata\":\"左\",\"TestPoint\":\"左\",\"Value\":\"0.305\"}]";
//        List<Machine_test> list = JacksonUtil.json2Bean(data, new TypeReference<List<Machine_test>>() {
//        });
//        String result = convert(list);
//        System.out.println(result);
//    }
   private static final List<String> includeKey =
           Arrays.asList("Height1","Height2","Height3","Width1","Width2","Width3","Width4","Width5",
                         "C1_X","C1_Y","C2_X","C2_Y","C2_D","C3_X","C3_Y","C3_D",
                         "S_L1","S_L2","S_L3","S_L4",
                         "D_L1","D_L2","D_R1","D_R2",
                         "D_U1","D_U2","D_U3","D_U4",
                         "D_D1","D_D2","D_D3","D_D4",
                         "D_LU1","D_LU2","D_LU3",
                         "D_RU1","D_RU2","D_RU3",
                         "D_LD1","D_LD2","D_LD3",
                         "H_L1","H_L2","H_R1","H_R2",
                         "H_U1","H_U2","H_U3","H_U4",
                         "H_D1","H_D2","H_D3","H_D4","H_D5",
                         "H_LU_1","H_LU_2","H_LU_3",
                         "H_RU_15","H_RU_45","H_RU_75",
                         "H_RD_15","H_RD_45","H_RD_75",
                         "H_LD_15","H_LD_45","H_LD_75",
                         "N1","N2","N3","N4","N5","N6","N7","N8","N9","N10",
                         "N11","N12","N13","N14","N15","N16","N17","N18","N19","N20",
                         "N21","N22","N23","N24","N25","N26","N27","N28","N29","N30",
                         "N31","N32","N33","N34","N35","N36","N37","N38","N39","N40",
                         "D_P1","D_P2","D_P3","D_P4","D_P5","D_P6","D_P7","D_P8"
                         );
          // Arrays.asList("左长-4.2","中长-3","右长-4.1","上宽","中宽","下宽","圆7","2号尺寸","圆3","4号尺寸","15号尺寸","圆9","3号尺寸");
    public static String convert(String batchqrcode,MachineTx machineTx ) throws UnsupportedEncodingException, NoSuchAlgorithmException {
       // Map<String,String> detail_data = new HashMap<>();
        String testPoint;
        Map<String,String> jsonValue00 = new HashMap<>();
        Map<String,String> jsonValue = new HashMap<>();
        jsonValue.put("barCode",machineTx.getQrcode());
        jsonValue.put("testResult",machineTx.getResult());
        jsonValue.put("testStartTime",machineTx.getCreate_time());
        jsonValue.put("testEndTime",machineTx.getCreate_time());
        jsonValue.put("deviceName","CCD");//设备名称
        jsonValue.put("factoryName","惠州纵胜");
        jsonValue.put("part","电池盖单体");
        jsonValue.put("projectName",machineTx.getItem_no());//项目号
        jsonValue.put("projectStatus","V4");
        jsonValue.put("objectColor","黑色");
        jsonValue.put("stackNo","");



        for (Machine_test machineTest : machineTx.getMachine_test()) {
            testPoint = machineTest.getTestPoint();

            if (testPoint.equals("外长1"))
                machineTest.setTestPoint("Height1");
            if (testPoint.equals("外长2"))
                machineTest.setTestPoint("Height2");
            if (testPoint.equals("外长3"))
                machineTest.setTestPoint("Height3");
            if (testPoint.equals("外宽1"))
                machineTest.setTestPoint("Width1");
            if (testPoint.equals("外宽2"))
                machineTest.setTestPoint("Width2");
            if (testPoint.equals("外宽3"))
                machineTest.setTestPoint("Width3");
            if (testPoint.equals("外宽4"))
                machineTest.setTestPoint("Width4");
            if (testPoint.equals("外宽5"))
                machineTest.setTestPoint("Width5");

            if (testPoint.equals("玻纤孔1位置度dx"))
                machineTest.setTestPoint("C1_X");
            if (testPoint.equals("玻纤孔1位置度dy"))
                machineTest.setTestPoint("C1_Y");
            if (testPoint.equals("玻纤孔1直径"))
                machineTest.setTestPoint("C1_D");
            if (testPoint.equals("玻纤孔2位置度dx"))
                machineTest.setTestPoint("C2_X");
            if (testPoint.equals("玻纤孔2位置度dy"))
                machineTest.setTestPoint("C2_Y");
            if (testPoint.equals("玻纤孔2直径"))
                machineTest.setTestPoint("C2_D");

            if (testPoint.equals("摄像头孔位置度dx"))
                machineTest.setTestPoint("C3_X");
            if (testPoint.equals("摄像头孔位置度dy"))
                machineTest.setTestPoint("C3_Y");
            if (testPoint.equals("摄像头孔直径"))
                machineTest.setTestPoint("C3_D");
            if (testPoint.equals("下长边直线度"))
                machineTest.setTestPoint("S_L1");
            if (testPoint.equals("上长边直线度"))
                machineTest.setTestPoint("S_L2");
            if (testPoint.equals("左短边直线度"))
                machineTest.setTestPoint("S_L3");
            if (testPoint.equals("右短边直线度"))
                machineTest.setTestPoint("S_L4");

            if (testPoint.equals("边高L1"))
                machineTest.setTestPoint("D_L1");
            if (testPoint.equals("边高L2"))
                machineTest.setTestPoint("D_L2");
            if (testPoint.equals("边高R1"))
                machineTest.setTestPoint("D_R1");
            if (testPoint.equals("边高R2"))
                machineTest.setTestPoint("D_R2");
            if (testPoint.equals("边高U1"))
                machineTest.setTestPoint("D_U1");
            if (testPoint.equals("边高U2"))
                machineTest.setTestPoint("D_U2");
            if (testPoint.equals("边高U3"))
                machineTest.setTestPoint("D_U3");
            if (testPoint.equals("边高U4"))
                machineTest.setTestPoint("D_U4");
            if (testPoint.equals("边高D1"))
                machineTest.setTestPoint("D_D1");
            if (testPoint.equals("边高D2"))
                machineTest.setTestPoint("D_D2");
            if (testPoint.equals("边高D3"))
                machineTest.setTestPoint("D_D3");
            if (testPoint.equals("边高D4"))
                machineTest.setTestPoint("D_D4");
            if (testPoint.equals("边高D5"))
                machineTest.setTestPoint("D_D5");

            if (testPoint.equals("拐角边高LU1"))
                machineTest.setTestPoint("D_LU1");
            if (testPoint.equals("拐角边高LU1"))
                machineTest.setTestPoint("D_LU2");
            if (testPoint.equals("拐角边高LU3"))
                machineTest.setTestPoint("D_LU3");
            if (testPoint.equals("拐角边高RU1"))
                machineTest.setTestPoint("D_RU1");
            if (testPoint.equals("拐角边高RU2"))
                machineTest.setTestPoint("D_RU2");
            if (testPoint.equals("拐角边高RU3"))
                machineTest.setTestPoint("D_RU3");
            if (testPoint.equals("拐角边高RD1"))
                machineTest.setTestPoint("D_RD1");
            if (testPoint.equals("拐角边高RD2"))
                machineTest.setTestPoint("D_RD2");
            if (testPoint.equals("拐角边高RD3"))
                machineTest.setTestPoint("D_RD3");
            if (testPoint.equals("拐角边高LD1"))
                machineTest.setTestPoint("D_LD1");
            if (testPoint.equals("拐角边高LD2"))
                machineTest.setTestPoint("D_LD2");
            if (testPoint.equals("拐角边高LD3"))
                machineTest.setTestPoint("D_LD3");

            if (testPoint.equals("L1"))
                machineTest.setTestPoint("H_L1");
            if (testPoint.equals("L2"))
                machineTest.setTestPoint("H_L2");
            if (testPoint.equals("R1"))
                machineTest.setTestPoint("H_R1");
            if (testPoint.equals("R2"))
                machineTest.setTestPoint("H_R2");
            if (testPoint.equals("U1"))
                machineTest.setTestPoint("H_U1");
            if (testPoint.equals("U2"))
                machineTest.setTestPoint("H_U2");
            if (testPoint.equals("U3"))
                machineTest.setTestPoint("H_U3");
            if (testPoint.equals("U4"))
                machineTest.setTestPoint("H_U4");
            if (testPoint.equals("D1"))
                machineTest.setTestPoint("H_D1");
            if (testPoint.equals("D2"))
                machineTest.setTestPoint("H_D2");
            if (testPoint.equals("D3"))
                machineTest.setTestPoint("H_D3");
            if (testPoint.equals("D4"))
                machineTest.setTestPoint("H_D4");
            if (testPoint.equals("D5"))
                machineTest.setTestPoint("H_D5");

            if (testPoint.equals("左上角15°"))
                machineTest.setTestPoint("H_LU_1");
            if (testPoint.equals("左上角45°"))
                machineTest.setTestPoint("H_LU_2");
            if (testPoint.equals("左上角75°"))
                machineTest.setTestPoint("H_LU_3");
            if (testPoint.equals("右上角15°"))
                machineTest.setTestPoint("H_RU_15");
            if (testPoint.equals("右上角45°"))
                machineTest.setTestPoint("H_RU_45");
            if (testPoint.equals("右上角75°"))
                machineTest.setTestPoint("H_RU_75");
            if (testPoint.equals("右下角15°"))
                machineTest.setTestPoint("H_RD_15");
            if (testPoint.equals("右下角45°"))
                machineTest.setTestPoint("H_RD_45");
            if (testPoint.equals("右下角75°"))
                machineTest.setTestPoint("H_RD_75");
            if (testPoint.equals("左下角15°"))
                machineTest.setTestPoint("H_LD_15");
            if (testPoint.equals("左下角45°"))
                machineTest.setTestPoint("H_LD_45");
            if (testPoint.equals("左下角75°"))
                machineTest.setTestPoint("H_LD_75");

            /*begin  部分佳视错误对照 20241205 李志鹏*/
            if (testPoint.equals("左上角15°-B"))
                machineTest.setTestPoint("H_LU_1");
            if (testPoint.equals("左上角45°-B"))
                machineTest.setTestPoint("H_LU_2");
            if (testPoint.equals("左上角75°-B"))
                machineTest.setTestPoint("H_LU_3");
            if (testPoint.equals("右上角15°-B"))
                machineTest.setTestPoint("H_RU_15");
            if (testPoint.equals("右上角45°-B"))
                machineTest.setTestPoint("H_RU_45");
            if (testPoint.equals("右上角75°-B"))
                machineTest.setTestPoint("H_RU_75");
            if (testPoint.equals("右下角15°-B"))
                machineTest.setTestPoint("H_RD_15");
            if (testPoint.equals("右下角45°-B"))
                machineTest.setTestPoint("H_RD_45");
            if (testPoint.equals("右下角75°-B"))
                machineTest.setTestPoint("H_RD_75");
            if (testPoint.equals("左下角15°-B"))
                machineTest.setTestPoint("H_LD_15");
            if (testPoint.equals("左下角45°-B"))
                machineTest.setTestPoint("H_LD_45");
            if (testPoint.equals("左下角75°-B"))
                machineTest.setTestPoint("H_LD_75");
            /*end  部分佳视错误对照 */


            if (testPoint.equals("四角线轮廓度N1"))
                machineTest.setTestPoint("N1");
            if (testPoint.equals("四角线轮廓度N2"))
                machineTest.setTestPoint("N2");
            if (testPoint.equals("四角线轮廓度N3"))
                machineTest.setTestPoint("N3");
            if (testPoint.equals("四角线轮廓度N4"))
                machineTest.setTestPoint("N4");
            if (testPoint.equals("四角线轮廓度N5"))
                machineTest.setTestPoint("N5");
            if (testPoint.equals("四角线轮廓度N6"))
                machineTest.setTestPoint("N6");
            if (testPoint.equals("四角线轮廓度N7"))
                machineTest.setTestPoint("N7");
            if (testPoint.equals("四角线轮廓度N8"))
                machineTest.setTestPoint("N8");
            if (testPoint.equals("四角线轮廓度N9"))
                machineTest.setTestPoint("N9");
            if (testPoint.equals("四角线轮廓度N10"))
                machineTest.setTestPoint("N10");

            if (testPoint.equals("四角线轮廓度N21"))
                machineTest.setTestPoint("N21");
            if (testPoint.equals("四角线轮廓度N22"))
                machineTest.setTestPoint("N22");
            if (testPoint.equals("四角线轮廓度N23"))
                machineTest.setTestPoint("N23");
            if (testPoint.equals("四角线轮廓度N24"))
                machineTest.setTestPoint("N24");
            if (testPoint.equals("四角线轮廓度N25"))
                machineTest.setTestPoint("N25");
            if (testPoint.equals("四角线轮廓度N26"))
                machineTest.setTestPoint("N26");
            if (testPoint.equals("四角线轮廓度N27"))
                machineTest.setTestPoint("N27");
            if (testPoint.equals("四角线轮廓度N28"))
                machineTest.setTestPoint("N28");
            if (testPoint.equals("四角线轮廓度N29"))
                machineTest.setTestPoint("N29");
            if (testPoint.equals("四角线轮廓度N30"))
                machineTest.setTestPoint("N30");

            if (testPoint.equals("四角线轮廓度N31"))
                machineTest.setTestPoint("N31");
            if (testPoint.equals("四角线轮廓度N32"))
                machineTest.setTestPoint("N32");
            if (testPoint.equals("四角线轮廓度N33"))
                machineTest.setTestPoint("N33");
            if (testPoint.equals("四角线轮廓度N34"))
                machineTest.setTestPoint("N34");
            if (testPoint.equals("四角线轮廓度N35"))
                machineTest.setTestPoint("N35");
            if (testPoint.equals("四角线轮廓度N36"))
                machineTest.setTestPoint("N36");
            if (testPoint.equals("四角线轮廓度N37"))
                machineTest.setTestPoint("N37");
            if (testPoint.equals("四角线轮廓度N38"))
                machineTest.setTestPoint("N38");
            if (testPoint.equals("四角线轮廓度N39"))
                machineTest.setTestPoint("N39");
            if (testPoint.equals("四角线轮廓度N40"))
                machineTest.setTestPoint("N40");

            if (testPoint.equals("火山口高度1"))
                machineTest.setTestPoint("D_P1");
            if (testPoint.equals("火山口高度2"))
                machineTest.setTestPoint("D_P2");
            if (testPoint.equals("火山口高度3"))
                machineTest.setTestPoint("D_P3");
            if (testPoint.equals("火山口高度4"))
                machineTest.setTestPoint("D_P4");
            if (testPoint.equals("火山口高度5"))
                machineTest.setTestPoint("D_P5");
            if (testPoint.equals("火山口高度6"))
                machineTest.setTestPoint("D_P6");
            if (testPoint.equals("火山口高度7"))
                machineTest.setTestPoint("D_P7");
            if (testPoint.equals("火山口高度8"))
                machineTest.setTestPoint("D_P8");

            jsonValue00.put(machineTest.getTestPoint(), machineTest.getValue());


            if(includeKey.contains(machineTest.getTestPoint())) {
                jsonValue.put(machineTest.getTestPoint(), machineTest.getValue());
            }

        }

     //   System.out.println(machineTx.getQrcode()+"---"+jsonValue00);
        System.out.println(machineTx.getQrcode()+"--a--"+jsonValue);
       /*
        for (Machine_test machineTest : machineTx.getMachine_test()) {
            if(includeKey.contains(machineTest.getTestPoint())) {
                jsonValue.put(machineTest.getTestPoint(), machineTest.getValue());
            }
        }*/

        //value为测试项json（详见2.1.3）gzip压缩之后的字符串，value值实现代码参考
       // String strValue =  jsonValue.toString();
        String strValue =  JacksonUtil.bean2Json(jsonValue);
      //  System.out.println("aaaaa"+strValue);
        GzipCompressJson _GzipCompressJson = new GzipCompressJson();
        strValue = _GzipCompressJson.compressString(strValue);

        //sha256为SHA256算法校验值，校验内容为barCode和value内容，数据上传时需填入，sha256值实现代码参考
        Sha256BarcodeValue _Sha256BarcodeValue = new Sha256BarcodeValue();
        String sha256 = _Sha256BarcodeValue.sha256(machineTx.getQrcode(), strValue);

        Map<String,String> jsonData = new HashMap<>();
        jsonData.put("barCode",machineTx.getQrcode());
        jsonData.put("value",strValue);
        jsonData.put("sha256",sha256);
        String strData =  JacksonUtil.bean2Json(jsonData);

        Map<String,String> uploadData = new HashMap<>();
        uploadData.put("materialType","COVER");
        uploadData.put("dataType","3D");
        uploadData.put("data",strData);

       // System.out.println(uploadData);
        return JacksonUtil.bean2Json(uploadData);
/*
    public static String convert(MachineTx machineTx ) {
        Map<String,String> uploadData = new HashMap<>();
        uploadData.put("barCode","ZYC02H2441500305");
        uploadData.put("testResult","OK");
        uploadData.put("testStartTime","20240419 10:25:17");
        uploadData.put("testEndTime","20240419 10:25:19");
        uploadData.put("factoryName","浙江公司");
        uploadData.put("deviceName","OMM");
        uploadData.put("projectName","CYB");
        uploadData.put("part","电池盖单体");
        uploadData.put("projectStatus","V4");
        uploadData.put("objectColor","黑色");
        uploadData.put("stackNo","");
        uploadData.put("BoxNo","");
*/


    }
}
